//app.js
App({
  onLaunch: function() {
    //调用API从本地缓存中获取数据
    let self = this
    var logs = wx.getStorageSync('logs') || []
    wx.setStorageSync('logs', logs)
    self.mapCtx = wx.createMapContext('myMap')
    logs.unshift(Date.now()) 
    self.getUserInfo()
    let location = self.globalData.location;
    if (!!!location) {
      self.getLocation();
    }
  },
  getOpenId(cb) {
    let self = this
    wx.login({
      complete: function (data) {
        self.query('wechat/login', { code: data.code }, 'post').then(user => {
          console.log(user)
          self.globalData.open_id = user.result.openid
          typeof cb == "function" && cb(self.globalData.open_id)
        })
      }
    })
  },
  //获取用户的经纬度
  getLocation() {
    let self = this;
    wx.getLocation({
      type: 'gps',
      success: function (res) {
        console.log(res)
        self.globalData.location = {};
        self.globalData.location.lat = res.latitude;
        self.globalData.location.lon = res.longitude
        // wx.openLocation({
        //   type:'gcj02',
        //   //当前经纬度
        //   latitude: res.latitude,
        //   longitude: res.longitude,
        //   //缩放级别默认28
        //   scale: 28,
        //   //成功打印信息
        //   success: function (res) {
        //     console.log(res)
        //   },
        //   //失败打印信息
        //   fail: function (err) {
        //     console.log(err)
        //   },
        //   //完成打印信息
        //   complete: function (info) {
        //     console.log(info)
        //   },
        // })
        // this.getCenterLocation()
      },
    })
  },
  // getCenterLocation: function () {
  //   this.mapCtx.getCenterLocation({
  //     success: function (res) {
  //       console.log(res.longitude)
  //       console.log(res.latitude)
  //     }
  //   })
  // },

  //获取用户
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      wx.login({
        success: function (wxlogin) {
          that.globalData.wxcode = wxlogin.code
          wx.getUserInfo({
            success: function (res) {
              console.log(res);
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      });
    }
  },
  //请求
  query(url, data = {}, method = 'GET', bool = false) {
    let apiUrl = "https://api.chuanggj.com/"
    let mydata = data
    let self = this
    let formaturl = apiUrl + url + '.json'
    let location = self.globalData.location;
    if (!!location) {
      data = Object.assign({}, data, location);
    }
    return new Promise(function (resolve, reject) {
      wx.request({
        url: formaturl,
        data: data,
        method: method,
        header: {
          Authorization: wx.getStorageSync('wechat_service_5mutian_token')
        }, 
        success: function (res) {
          let code = res.data.code
          if (code != 419) {
            resolve(res.data)
            if (code != 200 && !bool) { 
              return
            }
          } 
          else {
            console.log("jilaiole")
            wx.reLaunch({
              url: '../login/login'
            })
          }
        },
        fail: function (error) {
          resolve(error)
        },
        complete: function (all) {
          wx.hideToast();
        }
      })
    })
  },
  goBack: function (num) {
    wx.navigateBack({
      delta: num || 1
    })
  },
  globalData: {
    wxcode: null,
    systemInfo: null,
    userInfo: null,
    location: null,
    limit: 10,
    open_id:null,
    
  }
})
